package com.sohu.peak.util;

import java.net.URLEncoder;

public class ChineseSegmenterUtil {
	public static String uencode(String ch) throws Exception {
		String tmp = "";
		for (int i = 0; i < ch.length(); i++) {
			String tt = ch.substring(i, i + 1);
			tt = URLEncoder.encode(tt, "utf-16");
			tt = tt.replaceAll("%FE%FF", "");
			tt = "\\u" + tt.replaceAll("%", "");
			tt = tt.toLowerCase();
			tmp += tt;
		}
		tmp += ",";
		return tmp;
	}
	/**
	 * 判断传入的char值是否阿拉伯数字，支持全角/半角形式
	 * 
	 * @author 
	 * @since 2007-12-03
	 * @param ch
	 *            传入的char值
	 * @return 是数字则返回true
	 */
	public static final boolean isArabianNumber(char ch) {
		return ch >= '0' && ch <= '9' || ch >= '０' && ch <= '９';
	}

	/**
	 * 判断传入的char值是否英文字母，支持大小写、全角/半角形式
	 * 
	 * @author 
	 * @since 2007-12-03
	 * @param ch
	 *            传入的char值
	 * @return 是字母则返回true
	 */
	public static final boolean isLetter(char ch) {
		return ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch >= 'ａ'
				&& ch <= 'ｚ' || ch >= 'Ａ' && ch <= 'Ｚ';
	}

	/**
	 * @author 
	 * @since 2007-12-03 原方法不支持全角形式的数字和字母
	 */
	public static boolean isDigitorAlpha(char ch) {
		return isArabianNumber(ch) || isLetter(ch);
	}
	public static boolean isDAString(String chi) {
		boolean ismarket = true;

		for (int i = 0; i < chi.length(); i++) {
			if (!ChineseSegmenterUtil.isDigitorAlpha(chi.charAt(i))) {
				ismarket = false;
				break;
			}
		}
		return ismarket;
	}public static boolean isMarkString(String chi) {
		boolean ismarket = true;

		for (int i = 0; i < chi.length(); i++) {
			if (!ChineseSegmenterUtil.isMark(chi.charAt(i))) {
				ismarket = false;
				break;
			}
		}
		return ismarket;
	}
	/**
	 * @author 
	 * @since 2007-12-03 调用新的方法处理，原接口仍然可用
	 */
	public static boolean isAlpha(char ch) {
		return isLetter(ch);
	}

	// 保留中文、字母和数字
	public static String selectCJK(String ch) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < ch.length(); i++) {
			if ((Character.UnicodeBlock.of(ch.charAt(i)) == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS)
					|| (isDigitorAlpha(ch.charAt(i)))) {
				sb.append(ch.charAt(i));
			}
		}
		return sb.toString();
	}

	// 保留中文、字母和数字，其余替成空格
	public static String clearCJK(String ch) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < ch.length(); i++) {
			if ((Character.UnicodeBlock.of(ch.charAt(i)) == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS)
					|| (isDigitorAlpha(ch.charAt(i)))) {
				sb.append(ch.charAt(i));
			} else {
				sb.append(" ");
			}
		}
		return sb.toString();
	}/**
	 * 包含了全角数字和字母，绝大多数标点符号（除“”‘’"'。.）
	 */
	private static boolean isOtherMarks(char ch) {
		return ((ch >= 0xfb00) && (ch <= 0xfffd));
	}

	/**
	 * 原方法与isMark()部分功能重复，目前范围主要包含。（中文句号）
	 * 
	 * @since 2007-12-03
	 */
	private static boolean isChineseMark(char ch) {
		return Character.UnicodeBlock.of(ch) == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION;
	}

	/**
	 * 判断传入的char值是否标点符号（不包含数字、字母），支持全角/半角形式
	 * 
	 * @since 2007-12-03
	 * @param ch
	 *            传入的char值
	 * @return 是标点则返回true
	 */
	public static boolean isMark(char ch) {
		if (isChineseMark(ch) || (!isDigitorAlpha(ch) && isOtherMarks(ch))) {
			return true;
		}
		if ((ch == '“') || (ch == '”') || ch == '‘' || ch == '’' || ch == '・')

			return true;

		if (ch <= 128 && (!isDigitorAlpha(ch))) {
			return true;
		}
		return false;
	}

	public static String StrRidMark(String chi) {
		StringBuffer sb = new StringBuffer();
		if (chi != null) {
			for (int i = 0; i < chi.length(); i++) {
				if (!isMark(chi.charAt(i))) {
					sb.append(chi.charAt(i));
				}
			}
		}
		return sb.toString();
	}

	

	public static String getACJKString(String chi) {
		StringBuffer sb = new StringBuffer();

		for (int i = 0; i < chi.length(); i++) {
			if ((Character.UnicodeBlock.of(chi.charAt(i)) == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS)
					|| (isAlpha(chi.charAt(i)))) {
				sb.append(chi.charAt(i));
			}
		}
		return sb.toString();
	}

	public static int getACJKStringLength(String chi) {
		int count = 0;
		for (int i = 0; i < chi.length(); i++) {
			if ((Character.UnicodeBlock.of(chi.charAt(i)) == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS)
					|| (isAlpha(chi.charAt(i)))) {
				count++;
			}
		}
		return count;
	}

}
